package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin/setmeal")
@RestController
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmaelService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增的套餐信息:{}", setmealDTO);
        setmaelService.saveWithDish(setmealDTO);

        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageResult pageResult = setmaelService.pageQuery(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据ids批量删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(Long[] ids) {
        log.info("删除的套餐id为：{}", ids);
        setmaelService.delete(ids);
        return Result.success();
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据套餐id查询套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<SetmealVO> queryById(@PathVariable long id) {
        log.info("查询套餐的id为:{}", id);
        SetmealVO setmealVO = setmaelService.queryById(id);

        return Result.success(setmealVO);

    }

    /**
     * 根据数据修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PutMapping
    @ApiOperation("根据数据修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {

        setmaelService.update(setmealDTO);

        return Result.success();
    }

    /**
     * 修改套餐状态
     * @param status
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result stopOrStar(@PathVariable Integer status,Long id){
        setmaelService.stopOrStar(status,id);
        return Result.success();
    }

}
